package com.woniuxy.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @auther linke
 * @create 2021-10-21 14:28
 */
@WebServlet("/cookieDemo04")
public class CookieDemo04 extends HttpServlet {
    /**
     * cookie的生命周期
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("cookieDemo04...执行了");

        Cookie cookie = new Cookie("name", "tom");
        //设置cookie存活时间，单位S
        //cookie.setMaxAge(-1);//负数，关闭浏览器就消失
        //cookie.setMaxAge(0);//只在当前会话生效
        cookie.setMaxAge(44);//会在本地存储44S
        resp.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
